package com.newenergy.light.ble;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertisingSet;
import android.bluetooth.le.AdvertisingSetCallback;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.os.Build;
import android.util.Log;


/**
 *
 * Created by Administrator on 2016/8/16.
 */
public class BleAdvHelper {

    public static final String TAG = "LFX";
    private BluetoothAdapter mAdapter;

    private BluetoothLeAdvertiser advertiser;

    private AdvertiseSettings advertiseSettings;
    AdvertisingSetCallback mAdvertisingSetCallback;
    private AdvertiseCallback mAdvertiseCallback;

    public static BleAdvHelper getInstance() {
        return Wrapper.instance;
    }

    static class Wrapper {

        private static BleAdvHelper instance;

        static {
            instance = new BleAdvHelper();
        }
    }

    private BleAdvHelper() {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        getAdvertiseSettings();
    }

    private void getAdvertiseSettings() {
        AdvertiseSettings.Builder settingsBuilder = new
                AdvertiseSettings.Builder();
        settingsBuilder.setAdvertiseMode(AdvertiseSettings
                .ADVERTISE_MODE_LOW_LATENCY);//2
        settingsBuilder.setTxPowerLevel(AdvertiseSettings
                .ADVERTISE_TX_POWER_HIGH);//3//ADVERTISE_TX_POWER_HIGH
       settingsBuilder.setConnectable(true);
        settingsBuilder.setTimeout(0);
        advertiseSettings = settingsBuilder.build();
    }


    public void send(int manufacturerID, byte[] data, AdvertiseCallback callBack) {
        AdvertiseData advertiseData = getAdvertiseData(manufacturerID, data);
        if (advertiser != null) {
            advertiser = null;
        }
        advertiser = mAdapter.getBluetoothLeAdvertiser();

        if (advertiser != null) {
            this.advertiser.startAdvertising(advertiseSettings, advertiseData, null,callBack);
//            if (Build.VERSION.SDK_INT >= 26) {//  added in API level 26
//
//               /* AdvertisingSetParameters.Builder builder = new AdvertisingSetParameters.Builder();
//
//                // builder.setLegacyMode(false)
//                AdvertisingSetParameters parameters = builder
//                        .setLegacyMode(true)
//                        .setTxPowerLevel(AdvertisingSetParameters
//                                .TX_POWER_HIGH)
//                        .setInterval(AdvertisingSetParameters.INTERVAL_LOW)
////                        .setAnonymous(true)
//                         .setConnectable(true)
//                        .setScannable(false)
//                        .build();
//
//                boolean isLegacy = parameters.isLegacy();
//                Log.e(TAG, "isLegacy: "+isLegacy );*/
//
////                this.advertiser.startAdvertising(parameters, advertiseData, null, null, null, getAdvertisingSetCallback());
//                this.advertiser.startAdvertising(advertiseSettings, advertiseData, null,getAdvertisingCallback());
//            } else {
//                this.advertiser.startAdvertising(advertiseSettings, advertiseData, getAdvertisingCallback());
//            }

        } else {
            AdvertiserManager.getInstance().showToast("手机不支持蓝牙广播");
        }
        advertiseData = null;
    }

    public AdvertiseCallback getAdvertisingCallback() {
        if (mAdvertiseCallback != null) return mAdvertiseCallback;
//        Build.VERSION.SDK_INT < 26
        if (true) {
            mAdvertiseCallback = new AdvertiseCallback() {
                @Override
                public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    super.onStartSuccess(settingsInEffect);
                    Log.i(TAG, "onStartSuccess:");
                }

                @Override
                public void onStartFailure(int errorCode) {
                    super.onStartFailure(errorCode);
                    Log.i(TAG, "onStartFailure: errorCode = " + errorCode);
                }
            };
        }
        return mAdvertiseCallback;
    }

    public AdvertisingSetCallback getAdvertisingSetCallback() {
        if (mAdvertisingSetCallback != null) {
            return mAdvertisingSetCallback;
        }
        if (Build.VERSION.SDK_INT >= 26) {
            mAdvertisingSetCallback = new AdvertisingSetCallback() {
                @Override
                public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
                    super.onAdvertisingSetStarted(advertisingSet, txPower, status);
                    Log.i(TAG, "onAdvertisingSetStarted: ");
                }

                @Override
                public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
                    super.onAdvertisingSetStopped(advertisingSet);
                    Log.i(TAG, "onAdvertisingSetStopped: ");
                }


                @Override
                public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
                    super.onAdvertisingDataSet(advertisingSet, status);
                    Log.i(TAG, "onAdvertisingDataSet: ");
                }
            };
        }
        return mAdvertisingSetCallback;
    }


    private AdvertiseData getAdvertiseData(int manufacturerID, byte[] data) {
        AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
        dataBuilder.setIncludeDeviceName(false);
        dataBuilder.addManufacturerData(manufacturerID, data);
        return dataBuilder.build();
    }



    public void stop(AdvertiseCallback callback) {
        if (this.advertiser != null) {
            this.advertiser.stopAdvertising(callback);
        }

    }

}